//
//  ConcernViewController.m
//  项目三
//
//  Created by rimi1 on 2016/12/9.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "ConcernViewController.h"
#import "OtherPersonalViewController.h"
#import "CommentViewController.h"
#import "SignInViewController.h"

#import "FindTableViewCell.h"
#import "NotView.h"

#import "AVRequest+Relation.h"

@interface ConcernViewController () <UITableViewDelegate, UITableViewDataSource, FindTableViewCellDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <AVObject *> *dynamicArr;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NotView *notView;

@end

@implementation ConcernViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeAppearance];
}

#pragma mark - initialize

- (void)initializeDataSource {
    self.page = 1;
    self.dynamicArr = [NSMutableArray array];
    [self urlDynamicRequestWithIs:YES];
}

- (void)initializeAppearance {
    self.navigationBar.title.text = @"关注动态";
    [self.view addSubview:self.tableView];
}

#pragma mark - URLRequest

- (void)urlDynamicRequestWithIs:(BOOL)is {
    [AVRequest avConcernDynamicWithPage:self.page Query:^(NSArray *objects, NSError *error) {
        if (error) {
            self.notView.title.text = @"请求出错";
            [self.notView addSubview:self.notView.refresh];
            [self.view addSubview:self.notView];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            return;
        }
        // 当没有任何数据，就是没有任何关注时
        if (self.dynamicArr.count == 0 && objects.count == 0) {
            [self.notView.refresh removeFromSuperview];
            self.notView.title.text = @"您还没有关注任何好友！";
            [self.view addSubview:self.notView];
            return;
        }
        
        [self.notView removeFromSuperview];
        // 当上拉下载，没有数据时
        if (objects.count == 0 && self.page != 1) {
            [[RequestError sharedError] showErrorWithTitle:@"没有更多数据了"];
            self.page -= 1;
            [self.tableView.mj_footer endRefreshing];
            return;
        }
        
        for (NSInteger i = objects.count - 1; i >= 0; i--) {
            [self.dynamicArr addObject:objects[i]];
        }
        [self.tableView reloadData];
        if (is) {
            [self.tableView.mj_header endRefreshing];
        }else {
            [self.tableView.mj_footer endRefreshing];
        }
        
    }];
}

- (void)downPullRefresh {
    self.page = 1;
    self.dynamicArr = [NSMutableArray array];
    [self urlDynamicRequestWithIs:YES];
}

- (void)upPullLoad {
    self.page++;
    [self urlDynamicRequestWithIs:NO];
}

#pragma mark - FindTableViewCellDelegate
// 点击头像
- (void)findTableViewCell:(FindTableViewCell *)findTableViewCell tapOfHead:(UITapGestureRecognizer *)gestues{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:findTableViewCell];
    AVObject *obj = self.dynamicArr[indexPath.row][@"user"];
    
    OtherPersonalViewController *oPersonal = [[OtherPersonalViewController alloc] init];
    oPersonal.hidesBottomBarWhenPushed = YES;
    oPersonal.obj = obj;
    [self.navigationController pushViewController:oPersonal animated:YES];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dynamicArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reusing = @"findCell";
    FindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusing];
    if (!cell) {
        cell = [[FindTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusing];
    }
    AVObject *obj = self.dynamicArr[indexPath.row];
    
    NSString *head = obj[@"user"][@"headImage"];
    NSURL *url = [NSURL URLWithString:head];
    [cell.headImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"个人"] options:SDWebImageProgressiveDownload];
    cell.userName.text = obj[@"user"][@"name"];
    cell.menuName.text = obj[@"name"];
    
    NSString *backImg = obj[@"img"];
    NSURL *backUrl = [NSURL URLWithString:backImg];
    [cell.img sd_setImageWithURL:backUrl placeholderImage:nil options:SDWebImageProgressiveDownload];
    cell.food.text = [NSString stringWithFormat:@"食材：%@", obj[@"food"]];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AVObject *obj = self.dynamicArr[indexPath.row];
    CommentViewController *comment = [[CommentViewController alloc] init];
    comment.obj = obj;
    comment.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:comment animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return WXQAdaptionHeight(260);
}

#pragma mark - get

- (NotView *)notView {
    
    if (!_notView) {
        
        _notView = [[NotView alloc] initWithFrame:self.tableView.frame];
        _notView.backgroundColor = [UIColor whiteColor];
        _notView.title.text = @"您还没有关注任何好友！";
        [_notView.refresh removeFromSuperview];
        
        __weak ConcernViewController *concern = self;
        _notView.tapRefresh = ^() {
            [concern downPullRefresh];
        };
        
    }
    return _notView;
}

- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kWIDTH, kHEIGHT - 64) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downPullRefresh)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upPullLoad)];
        
    }
    return _tableView;
}

@end












